<?php

namespace App\Admin\Controllers;

use App\Models\Cabinet;
use App\Models\CabinetAlarm;
use App\Models\CabinetPoint;
use App\Models\CabinetAlarmLog;
use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
// use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use App\Models\ProjectUser;
use App\Models\Project;
use Dcat\Admin\Admin;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use App\Admin\Actions\Grid\HandleCabinetAlarmLog;

class CabinetAlarmLogController extends Controller
{
    public function index(Content $content, $boxId = 0)
    {
        Admin::script(
            <<<JS
    var table = document.getElementById('grid-table');
    table.style.cssText="background-color:#e3e5eb;"
JS            
        );
        return $content
            ->body(headmenu($boxId,5))
            ->body($this->grid($boxId));
    }

    protected function grid($boxId)
    {
        $optionBoxes = Cabinet::whereIn('id',[$boxId])->pluck('name', 'id')->toArray();
        $optionCabinets = Cabinet::where('pid', $boxId)->where('show', Cabinet::SHOW_YES)->pluck('name', 'id')->toArray();
        $cabinets = Cabinet::where('pid', $boxId)->pluck('id')->toArray();
        $optionPoints = CabinetPoint::whereIn('cabinet_id',$cabinets)->pluck('name', 'id')->toArray();
        $m = CabinetAlarmLog::whereIn('cabinet_id', $cabinets)->orderBy('created_at', 'desc');
        return new Grid($m, function (Grid $grid) use ($optionBoxes,$optionCabinets, $optionPoints) {
            $grid->column('cabinet_pid', '云盒')->using($optionBoxes);
            $grid->column('cabinet_id', '设备')->using($optionCabinets);
            $grid->column('point_id', '采集点')->using($optionPoints);
            $grid->column('alarm_id','报警名称')->display(function ($alarmId) {
                return CabinetAlarm::where('id', $alarmId)->value('name');
            });
            $grid->column('alarm_level', '报警等级')->using(CabinetAlarm::LEVEL_TEXT);
            $grid->column('alarm_value','报警值');
            $grid->column('real_value','监测值');
            $grid->column('status', '是否处理')->using(CabinetAlarmLog::STATUS_TEXT);
            $grid->column('created_at', '报警时间');
            $grid->column('updated_at', '处理时间');

            $grid->disableCreateButton();
           
            $grid->filter(function (Grid\Filter $filter) use ($optionCabinets) {
            //     // 更改为 panel 布局
                $filter->panel();
                $filter->equal('cabinet_id', '设备')->select($optionCabinets)->load('point_id', '/api/point_id');
                $filter->equal('point_id', '采集点')->select()->load('alarm_id', '/api/alarm_id');
                $filter->like('alarm_id', '报警名称')->select();
                $filter->equal('alarm_level', '报警等级')->select(CabinetAlarm::LEVEL_TEXT);
                $filter->equal('status', '是否处理')->select(CabinetAlarmLog::STATUS_TEXT);
            });
            $grid->setActionClass(Grid\Displayers\Actions::class);
            $grid->disableEditButton();
            $grid->disableViewButton();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append(new HandleCabinetAlarmLog());
            });
        });
    }

    public function destroy($id)
    {
        return $this->form()->destroy($id);
    }

    public function cabinet_pid(Request $request)
    {
        $projectId = $request->get('q');

        return Cabinet::where('project_id', $projectId)->where('pid', 0)->get(['id', DB::raw('name as text')]);
    }

    public function alarm_id(Request $request)
    {
        $pointId = $request->get('q');
        return CabinetAlarm::where('point_id', $pointId)->get(['id', DB::raw('name as text')]);
    }
}